//
//  OBMomentViewController.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/5/2.
//  Copyright © 2018年 obally. All rights reserved.
//

#import "OBMomentViewController.h"
#import "OBMomentProfileView.h"
#import "OBMomentItemViewModel.h"
#import "OBMomentHeaderView.h"
#import "OBMomentFooterView.h"
#import "OBMomentContentCell.h"
@interface OBMomentViewController ()
/// viewModel
@property (nonatomic, readonly, strong) OBMomentViewModel *viewModel;
/// 个人信息view
@property (nonatomic, readwrite, strong) OBMomentProfileView *tableHeaderView;


@end

@implementation OBMomentViewController
- (void)dealloc
{
    OBDealloc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化视图
    [self setUpSubViews];
    // 初始化导航栏Item
    [self setupNavigationItem];

    
    
}
- (void)bindViewModel
{
    [super bindViewModel];
    @weakify(self);
    //事件处理
    [[self.viewModel.reloadSectionSubject deliverOnMainThread] subscribeNext:^(NSNumber *section) {
        @strongify(self);
        [UIView performWithoutAnimation:^{
            [self.tableView reloadSection:section.integerValue withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    }];
    [RACObserve(self.viewModel, profileViewModel) subscribeNext:^(OBMomentProfileViewModel *viewModel) {
        if (viewModel) {
            [self _setUpProfileView];
        }
        
    }];
    
    //用户信息获取
    [self.viewModel.userProfileCommand execute:nil];
}
#pragma mark - Override
- (UIEdgeInsets)contentInset{
    return UIEdgeInsetsMake(OB_IS_IPHONE_X?-40:-64, 0, 0, 0);
}
#pragma mark - 初始化子控件
/*
 *初始化视图
 */
- (void)setUpSubViews
{
    //配置tableView
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.sectionFooterHeight = OBMomentFooterViewHeight;
   
}
//个人信息View
- (void)_setUpProfileView
{
    //个人信息View
    OBMomentProfileView *tableHeaderView = [[OBMomentProfileView alloc]init];
    [tableHeaderView bindViewModel:self.viewModel.profileViewModel];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = tableHeaderView;
    self.tableView.tableHeaderView.mh_height = self.viewModel.profileViewModel.height;
    self.tableHeaderView = tableHeaderView;
}
#pragma mark - 初始化道导航栏
- (void)setupNavigationItem
{
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    return [OBMomentContentCell cellWithTableView:tableView];
}

- (void)configureCell:(OBMomentContentCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object{
    OBMomentItemViewModel *itemViewModel =  self.viewModel.dataSource[indexPath.section];
    id model = itemViewModel.dataSource[indexPath.row];
    [cell bindViewModel:model];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    OBMomentItemViewModel *itemViewModel =  self.viewModel.dataSource[section];
    return itemViewModel.dataSource.count;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    OBMomentHeaderView *headerView = [OBMomentHeaderView headerViewWithTableView:tableView];
//    /// 传递section
    headerView.section = section;
    [headerView bindViewModel:self.viewModel.dataSource[section]];
    return headerView;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [OBMomentFooterView footerViewWithTableView:tableView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    OBMomentItemViewModel *itemViewModel = self.viewModel.dataSource[indexPath.section];
    id object = itemViewModel.dataSource[indexPath.row];
    [self configureCell:cell atIndexPath:indexPath withObject:object];
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
/// 设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    OBMomentItemViewModel *itemViewModel = self.viewModel.dataSource[section];
    return itemViewModel.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OBMomentItemViewModel *itemViewModel = self.viewModel.dataSource[indexPath.section];
    id model = itemViewModel.dataSource[indexPath.row];
    return  [model cellHeight];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
